var js = {
    //当前打开的行
    expandIndex: -1,
    //新增状态
    formStatus: false,
    //判断按钮显示
    setBtnStatus: function () {
        var row = $('#js_grid').datagrid('getSelected');
        if (row && row.id) {
            $("#js_del").show();
        } else {
            $("#js_del").hide();
        }
    },
    //重置状态
    resetStatus: function () {
        js.setBtnStatus();
        js.expandIndex = -1;
        js.formStatus = false;
    }
}

$(function () {
    //表格初始加载
    $('#js_grid').datagrid({
        view: detailview,
        detailFormatter: function () {
            return '<div class="detail"></div>';
        },
        onExpandRow: function (index, row) {
            $("#layout_js").mask();
            //关闭展开的行
            if (js.expandIndex != -1) {
                $('#js_grid').datagrid("collapseRow", js.expandIndex);
            }
            //记录当前展开哪行
            js.expandIndex = index;
            show_js_form(index, row);//加载表格面板
        },
        onCollapseRow: function () {
            //删除当前记录的行
            js.expandIndex = -1;
        },
        onLoadSuccess: js.setBtnStatus,
        onClickRow: js.setBtnStatus,
        onSelect: js.setBtnStatus,
        onDblClickCell: function (index, field) {
            //双击事件
            if (field == "mc") {
                $('#js_grid').datagrid("expandRow", index);
            }
        }
    });

//*************按钮触发事件***********
//新增触发
    $('#js_add').click(
        js_newItem
    );
//删除触发
    $('#js_del').click(
        js_destroyItem
    );
//搜索触发
    $('#js_search').click(
        js_search
    );
//*************按钮触发事件***********
});

//新增角色
function js_newItem() {
    //关闭打开的行
    if (js.expandIndex != -1) {
        $('#js_grid').datagrid("collapseRow", js.expandIndex);
    }

    //已有新增判断
    if (!js.formStatus) {
        $('#js_grid').datagrid('insertRow', {index: 0, row: {isNewRecord: true}});
        js.formStatus = true;
    }
    $('#js_grid').datagrid('expandRow', 0);
//    $('#js_grid').datagrid('getRowDetail', 0).find("[name='js.qxList.id']").qxWin();

    $('#js_grid').datagrid('selectRow', 0);
}

// 保存角色新增表单
function js_saveItem(index) {
    $("#layout_js").mask();
    $('#js_grid').datagrid('getRowDetail', index).find('form').form('submit', {
        url: $.webPath() + '/sys/js/add_js',
        onSubmit: function () {
            var isValid = $(this).form('validate');
            if (!isValid) {
                $("#layout_js").mask('hide');
                return false;
            }
            return isValid;
        },
        success: function (data) {
            $("#layout_js").mask("hide");
            var obj = eval('(' + data + ')');
            if (obj.statusCode == 200) {
                $('#js_grid').datagrid('reload');
                js.resetStatus();
                $.messager.show({
                    title: '提示',
                    msg: obj.message,
                    timeout: 3000,
                    showType: 'slide'
                });
            } else
                $.messager.alert('提示', obj.message, 'info');
        }
    });

}

//取消角色表单
function js_cancelItem(index) {
    var row = $('#js_grid').datagrid('getRows')[index];
    if (row.isNewRecord) {
        $('#js_grid').datagrid('deleteRow', index);
    } else {
        $('#js_grid').datagrid('collapseRow', index);
    }
    $('#js_grid').datagrid('reload');
    js.resetStatus();
}
//删除角色
function js_destroyItem() {
    var row = $('#js_grid').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否删除该角色', function (r) {
            if (r) {
                var index = $('#js_grid').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/sys/js/delete_js",
                    dataType: 'json',
                    type: 'POST',
                    data: {jsId: row.id},
                    beforeSend: function () {
                        $("#layout_js").mask();
                    },
                    success: function (data) {
                        $("#layout_js").mask("hide");
                        if (data.statusCode == 200) {
                            $('#js_grid').datagrid('reload');
                            $('#js_grid').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                            js.resetStatus();
                        } else {
                            $.messager.alert('提示', data.message, 'error');
                        }
                    }
                });
//                $.post($.webPath() + '/sys/js/delete_js', {jsId:row.id}, function (data) {
//                    var obj = eval('(' + data + ')');
//                    if (obj.statusCode == 200) {
//                        $('#js_grid').datagrid('deleteRow', index);
//                            $.messager.show({
//                                title:'提示',
//                                msg:obj.message,
//                                timeout:3000,
//                                showType:'slide'
//                            });
//                         js.resetStatus();
//                    }else {
//                        $.messager.alert('提示', obj.message, 'info');
//                    }
//                });
            }
        });
    } else {
        alert("请选择要删除的角色");
    }
}


//查询角色
function js_search() {
    $('#js_grid').datagrid('load', {
        mc: $('#js_mc').val()
    });
}

//展开角色表单
function show_js_form(index, row) {
   // alert(index);
    $("#layout_js").mask("hide");
    var ddv = $("#js_grid").datagrid('getRowDetail', index).find('div.detail');
    var url = $.webPath() + '/sys/js/js_content?index=' + index;
    if (row.id)
        url += '&jsId=' + row.id;
    ddv.panel({
        border: false,
        cache: true,
        href: url,
        onLoad: function () {
            $('#js_grid').datagrid('fixDetailRowHeight', index);
            $('#js_grid').datagrid('selectRow', index);
            if (row.qxList_id != null) {
                var arrId = row.qxList_id.split(",");
                var arrMc = row.qxList.split(",");
                var arr = [];
                for (var i = 0; i < arrId.length; i++) {
                    arr.push({id: arrId[i], mc: arrMc[i]});
                }

                var qxWin = $('#js_grid').datagrid('getRowDetail', index).find("[name='js.qxList.id']");
                qxWin.qxWin({selected: arr});
            }else{
                var qxWin = $('#js_grid').datagrid('getRowDetail', index).find("[name='js.qxList.id']");
                qxWin.qxWin();
            }
            js.setBtnStatus();
        }
    });
    $('#js_grid').datagrid('fixDetailRowHeight', index);
}